<template>
  <g-layout :isShowLeftSider="false">
    <template #content>
      <a-tabs defaultActiveKey="1" :tabBarStyle="{ padding: '0 16px' }">
        <!-- 缓存 -->
        <a-tab-pane key="1" :tab="l('Caching')">
          <g-dynamic-table
            type="maintenance"
            ref="table"
            :extraHeight="60"
            :fetch="fetchDataList"
            :isPage="false"
            @action-click="handleActionClick"
          >
          </g-dynamic-table>
        </a-tab-pane>
        <!-- 网站日志 -->
        <a-tab-pane key="2" :tab="l('WebSiteLog')">
          <a-card :title="l('WebSiteLogsHeaderInfo')" :bordered="false">
            <template #extra>
              <a-button type="primary" @click="downloadWebLogs">
                <download-outlined />
                {{ l('DownloadAll') }}
              </a-button>
              <a-button type="primary" @click="getWeblogData">
                <reload-outlined />
                {{ l('Refresh') }}
              </a-button></template
            >
            <div class="web-log-view">
              <p v-for="(item, index) in weblogList" :key="index">{{ item }}</p>
            </div>
          </a-card>
        </a-tab-pane>
      </a-tabs>
    </template>
  </g-layout>
</template>

<script lang="ts">
  import GDynamicTable from '/@/shared/components/g-dynamic-table/index.vue';
  import { AppComponentBase } from '/@/shared/component-base';
  import {
    EntityDtoOfString,
    HostCachingServiceProxy,
    WebSiteLogServiceProxy,
  } from '/@/shared/service-proxies';
  import { EVENT_BTN_ENUM } from '/@/shared/components/g-dynamic-table/enums/event-btn-enum';
  import { fileDownloadService } from '/@/shared/utils';
  import { defineComponent } from 'vue';
  export default defineComponent({
    name: 'maintenance-news',
    components: {
      GDynamicTable,
    },
    mixins: [AppComponentBase],
    data() {
      return {
        // 缓存
        hostCachingServiceProxy: new HostCachingServiceProxy(),
        webSiteLogServiceProxy: new WebSiteLogServiceProxy(),
        // web日志
        weblogList: [] as string[],
      };
    },

    methods: {
      /**
       * 按钮回调事件
       */
      handleActionClick(event: string, item?: any) {
        switch (event) {
          case EVENT_BTN_ENUM.CLEAR:
            this.clearCache(item);
            break;
          case EVENT_BTN_ENUM.CLEAR_ALL:
            this.clearAllCaches();
            break;
          default:
            break;
        }
      },
      //获取数据
      fetchDataList() {
        return new Promise((resolve, reject) => {
          this.hostCachingServiceProxy
            .getAllCaches()
            .finally(() => {
              // finishedCallback!();
            })
            .then((res) => {
              resolve(res.items);
              // this.showPaging(res);
            })
            .catch((error) => {
              reject(error);
            });
        });
      },
      /**
       * 获取网站log
       */
      getWeblogData() {
        this.loading = true;
        this.webSiteLogServiceProxy
          .getLatestWebLogs()
          .finally(() => {
            this.loading = false;
          })
          .then((res) => {
            this.weblogList = res.latestWebLogLines as any;
          });
      },
      /**
       * 单个清空缓存
       */
      clearCache(item) {
        this.loading = true;
        let entitys = new EntityDtoOfString();
        entitys.id = item.name;
        this.hostCachingServiceProxy
          .clearCache(entitys)
          .finally(() => {
            this.loading = false;
          })
          .then(() => {
            this.notify.success(this.l('CacheSuccessfullyCleared'));
            (this.$refs.table as any).reloadGoFirstPage();
          });
      },
      /**
       * 清理全部缓存
       */
      clearAllCaches() {
        this.loading = true;
        this.hostCachingServiceProxy
          .clearAllCaches()
          .finally(() => {
            this.loading = false;
          })
          .then(() => {
            this.notify.success(this.l('AllCachesSuccessfullyCleared'));
            (this.$refs.table as any).reloadGoFirstPage();
          });
      },
      /**
       * 下载日志
       */
      downloadWebLogs() {
        this.loading = true;
        this.webSiteLogServiceProxy
          .downloadWebLogs()
          .finally(() => {
            this.loading = false;
          })
          .then((result) => {
            fileDownloadService.downloadTempFile(result);
          });
      },
    },
  });
</script>
<style scoped lang="less">
  .maintenance-container {
    padding: 16px;
  }
  .ant-tabs {
    background-color: #fff;
  }
  :deep(.dynamic-table-layout) {
    padding: 0;

    .toolbar-area {
      .group-container {
        padding-top: 0px;
      }
    }
  }

  :deep(.ant-card-head) {
    padding: 0 16px;
  }
  :deep(.ant-card-extra),
  :deep(.ant-card-head-title) {
    padding: 0;
  }

  .web-log-view {
    max-height: 400px;
    overflow-y: auto;
    overflow-x: hidden;

    p {
      word-break: break-all;
    }
  }
</style>
